package com.meida.module.bus.provider.handler;

import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusReport;
import com.meida.module.bus.provider.service.BusMaterialPackage100Service;
import com.meida.module.bus.provider.service.BusMaterialPackage200Service;
import com.meida.module.bus.provider.service.BusMaterialService;
import com.meida.module.bus.provider.service.BusProjectSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 月报
 */
@Component("busReportPageListHandler")
public class BusReportPageListHandler implements PageInterceptor<BusReport> {
    @Autowired
    private BusProjectSupplierService busProjectSupplierService;
    @Autowired
    private BusMaterialService busMaterialService;
    @Autowired
    private BusMaterialPackage100Service busMaterialPackage100Service;
    @Autowired
    private BusMaterialPackage200Service busMaterialPackage200Service;


    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
    }


    @Override
    public void complete(CriteriaQuery<BusReport> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            Integer reportType = entityMap.getInt("reportType");
            Long materialId = entityMap.getLong("materialId");
            if (reportType == 1) {
                EntityMap busMaterialPackage100 = busMaterialPackage100Service.findById(materialId);
                if (FlymeUtils.isNotEmpty(busMaterialPackage100)) {
                    initData(entityMap, busMaterialPackage100);
                }
            }
            if (reportType == 2) {
                EntityMap busMaterialPackage200 = busMaterialPackage200Service.findById(materialId);
                if (FlymeUtils.isNotEmpty(busMaterialPackage200)) {
                    initData(entityMap, busMaterialPackage200);
                }
            }
        }
    }

    private void initData(EntityMap entityMap, EntityMap busMaterialPackage) {
        Long materialPackage100Id = busMaterialPackage.getLong("materialPackage100Id");
        EntityMap materialEntityMap = busMaterialService.findById(materialPackage100Id);
        Long projectId = busMaterialPackage.getLong("projectId");
        String majorCode = busMaterialPackage.get("majorCode");
        String packageNo = busMaterialPackage.get("packageNo");
        Long supplierId = entityMap.getLong("supplierId");
        Map<String, Object> map = new HashMap<>();
        map.put("projectId", projectId);
        map.put("majorCode", majorCode);
        map.put("packageNo", packageNo);
        BigDecimal contractBillQuantity = materialEntityMap.getBigDecimal("contractBillQuantity");
        //本月完成工程量
        BigDecimal currentFinishMonthCount = materialEntityMap.getBigDecimal("currentFinishMonthCount");
        //截止上个月完成工程量
        BigDecimal lastFinishMonthCount = materialEntityMap.getBigDecimal("lastFinishMonthCount");
        //累计完成工程量
        BigDecimal totalFinishCount = materialEntityMap.getBigDecimal("totalFinishCount");
        if (contractBillQuantity.compareTo(BigDecimal.ZERO) == 0) {
            entityMap.put("nowMonthProportion", 0);
            entityMap.put("lastMonthProportion", 0);
            entityMap.put("totalProportion", 0);
        } else {
            String nowMonthProportion = currentFinishMonthCount.divide(contractBillQuantity, 10, BigDecimal.ROUND_UP).stripTrailingZeros().toPlainString();
            entityMap.put("nowMonthProportion", nowMonthProportion);
            String lastMonthProportion = lastFinishMonthCount.divide(contractBillQuantity, 10, BigDecimal.ROUND_UP).stripTrailingZeros().toPlainString();
            entityMap.put("lastMonthProportion", lastMonthProportion);
            String totalProportion = totalFinishCount.divide(contractBillQuantity, 10, BigDecimal.ROUND_UP).stripTrailingZeros().toPlainString();
            entityMap.put("totalProportion", totalProportion);
        }
        List<EntityMap> supplierIdList = busProjectSupplierService.getBusProjectSupplierIds(map);
        for (int i = 0; i < supplierIdList.size(); i++) {
            EntityMap idInfo = supplierIdList.get(i);
            Long id = idInfo.getLong("supplierId");
            if (id.longValue() == supplierId.longValue()) {
                int num = idInfo.getInt("index");
                String supplierNumId = "supplierNum" + num;
                BigDecimal supplierNum = busMaterialPackage.getBigDecimal(supplierNumId);
                entityMap.put("supplierNum", supplierNum);
            }
        }
        entityMap.putAll(materialEntityMap);
        entityMap.putAll(busMaterialPackage);
    }

}
